﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Configuration;
using LapvComponent;
using LapvWebComponent;
using MemcachedProviders.Cache;

namespace LapvBussiness
{
    public class Provider
    {
     
        public static string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }
        public static List<district> GetListDistrict(int provinceId)
        {
            try
            {
                List<district> lst = null;
                string cachekey = "District";
                if (provinceId > 0) cachekey = "District" + provinceId;
                object cached = null;
                try
                {
                    cached = DistCache.Get(cachekey);
                }
                catch (Exception)
                {
                }

                if (cached != null)
                {
                    lst = (List<district>)cached;
                }
                else
                {
                    using (lapvwebEntities db = new lapvwebEntities())
                    {
                        if (provinceId > 0)
                            lst = db.districts.Include("province").Where(ec => ec.province.id == provinceId).OrderByDescending(en => en.position).ToList();
                        else lst = db.districts.Include("province").OrderByDescending(en => en.position).ToList();
                    }
                    try
                    {
                        DistCache.Add(cachekey, lst);
                    }
                    catch (Exception)
                    {
                    }

                }

                return lst;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        
        public static void CheckAuthen(string returnUrl)
        {
            string domain = ConfigLapv.Instance.DomainAdmin;
            if ((HttpContext.Current.User.Identity.Name == "") || (HttpContext.Current.User.Identity.Name == null))
            {
                HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Redirect(domain+"Login.aspx?returnurl=" + returnUrl, false);
            }
        }
        public static LapvWebComponent.account CheckLogin(string user, string pass, out string errcode, lapvwebEntities db)
        {
            LapvWebComponent.account obj = null;

            try
            {

                obj = db.accounts.Include("account_profile").Where(ec => ec.username == user).FirstOrDefault();                    //&& ec.password == pass && ec.block == false
                if (obj != null)
                {
                    account_profile acc_pr = obj.account_profile;
                    if (acc_pr != null)
                    {
                        if (obj.block == true)
                        {
                            errcode = "Tài khoản đã bị khóa";
                        }
                        else if (obj.password != pass)
                        {
                            errcode = "Mật khẩu không đúng";
                        }
                        else
                        {
                            errcode = "";
                            obj.endlogin = DateTime.Now;
                            db.ApplyCurrentValues(obj.EntityKey.EntitySetName, obj);
                            db.SaveChanges();
                        }
                    }
                    else
                        errcode = "Chưa tồn tại tài khoản";
                }
                else
                {
                    errcode = "Tên đăng nhập không đúng";
                }



            }
            catch (Exception ex)
            {
                throw ex;
            }
            return obj;

        }
       
        public static List<news_category> GetListNewsCate()
        {
            List<news_category> lst = null;
            try
            {
                using (lapvwebEntities db = new lapvwebEntities())
                {
                    lst = db.news_category.ToList();
                                      
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            return lst;
        }
        public static LapvWebComponent.account GetUserByUserName(string username)
        {
            LapvWebComponent.account objAcc = null;
            try
            {
                using (lapvwebEntities db = new lapvwebEntities())
                {
                    if (!string.IsNullOrEmpty(username))
                    {
                        objAcc = db.accounts.Include("account_profile").Where(ec => ec.username == username).FirstOrDefault();
                        if (objAcc != null)
                        {
                            return objAcc;
                        }
                    }
                    return objAcc;
                }
            }
            catch (Exception ex)
            {

                throw ex;

            }

        }
        public static void AutoSetStatusNews()
        {
            try
            {
                using (lapvwebEntities db = new lapvwebEntities())
                {
                    DateTime dtnow= DateTime.Now;
                    List<news> lst = db.news.Where(ec => ec.status == 2 && (ec.starttimeup.Year == dtnow.Year && ec.starttimeup.Month == dtnow.Month && ec.starttimeup.Day == dtnow.Day && ec.starttimeup.Hour == dtnow.Hour && ec.starttimeup.Minute == dtnow.Minute)).ToList();
                    if (lst != null && lst.Count > 0)
                    {
                        foreach (var item in lst)
                        {
                            item.status = 1;
                            item.starttimeup = DateTime.MinValue;
                            db.ApplyCurrentValues(item.EntityKey.EntitySetName, item);
                        }
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }
    }
      
}
